/**
 * <p>
 * 版权所有：中企动力
 * </p>
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 * </p>
 */
package com.ce.pms.renovation.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.house.vo.BuildingVO;
import com.ce.pms.renovation.service.IInspectionService;
import com.ce.pms.renovation.service.IRenovateManageService;
import com.ce.pms.renovation.vo.FirstInspectionVO;
import com.ce.pms.renovation.vo.InspectionRequisitionQueryConditionVO;
import com.ce.pms.renovation.vo.InspectionRequisitionVO;
import com.ce.pms.renovation.vo.ReinspectionVO;
import com.ce.pms.renovation.vo.RenovateRequisitionVO;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Out;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.annotation.Scope;
import com.sitechasia.webx.core.support.Page;

/**
 * 装修验收
 *
 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
 * @Created 2008-12-24
 */

public class InspectionRequisitionAction extends PmsBaseAction {

	/**
	 * 添加页面
	 */
	private final String			ADD_PAGE	= "inspectionRequisitionAdd";

	/**
	 * 楼栋Service
	 */
	private IBuildingService		buildingService;

	private final String			DETAIL_PAGE	= "inspectionRequisitionDetail";

	/**
	 * 编辑页面
	 */
	private final String			EDIT_PAGE	= "inspectionRequisitionEdit";

	/**
	 * 初始化页面
	 */
	private final String			INIT_PAGE	= "inspectionRequisitionMain";

	/**
	 * 装修验收Service
	 */
	private IInspectionService		inspectionService;

	/**
	 * 列表页面
	 */
	private final String			LIST_PAGE	= "inspectionRequisitionList";

	private IRenovateManageService	renovateManageService;

	private final String			SEARCH_PAGE	= "inspectionRequisitionSearch";

	/**
	 * 添加操作
	 *
	 * @param inspectionRequisitionVO
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-29
	 */
	public void addInspectionRequisition(
			@Read InspectionRequisitionVO inspectionRequisitionVO) {
		inspectionRequisitionVO.setCreator(this.getUserId());

		if (!BlankUtil.isBlank(inspectionRequisitionVO.getInspecteDateStr())) {
			inspectionRequisitionVO.setInspecteDate(DateUtil.convertStrToDate(
					inspectionRequisitionVO.getInspecteDateStr(),
					"yyyy-MM-dd HH时"));
		}

		this.inspectionService
				.addInspectionRequisition(inspectionRequisitionVO);
		this.renderText("true");
	}

	/**
	 * 添加页面
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-26
	 */
	public String addPage() {
		return this.ADD_PAGE;
	}

	/**
	 * @param houseNumber
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-9
	 */
	public void checkHouseNumber(@Read(key = "houseNumber") String houseNumber) {
		this.renderText("true");
	}

	/**
	 * 删除装修验收
	 *
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-26
	 */
	public void deleteInspectionRequisition(@Read(key = "keyword") Long keyword) {
		try {
			if (this.inspectionService.deleteInspectionRequisition(keyword)) {
				this.renderText("true");
			} else {
				this.renderText("false");
			}
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw re;
		}
	}

	/**
	 * 编辑操作
	 *
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-26
	 */
	public void editInspectionRequisition(
			@Read InspectionRequisitionVO inspectionRequisitionVO) {
		inspectionRequisitionVO.setModifier(this.getUserId());

		if (!BlankUtil.isBlank(inspectionRequisitionVO.getInspecteDateStr())) {
			inspectionRequisitionVO.setInspecteDate(DateUtil.convertStrToDate(
					inspectionRequisitionVO.getInspecteDateStr(),
					"yyyy-MM-dd HH时"));
		}

		this.inspectionService
				.editInspectionRequisition(inspectionRequisitionVO);
		this.renderText("true");
	}

	/**
	 * 编辑页面
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-26
	 */
	public String editPage(@Read(key = "keyword") Long keyword) {
		InspectionRequisitionVO inspectionRequisitionVO = this.inspectionService
				.getInspectionRequisition(keyword);
		this.setAttribute("inspectionRequisitionVO", inspectionRequisitionVO);
		return this.EDIT_PAGE;
	}

	/**
	 * 获取房屋信息
	 *
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-29
	 */
	public void getHouseData(@Read(key = "houseId") Long houseId) {
		try {
			RenovateRequisitionVO renovateRequisitionVO = this.renovateManageService
					.getRequisitionOutline(houseId);
			String jsoneTmp = "{'houseId':'%1$s','companyNameValue':'%2$s', 'renovateLicenceValue':'%3$s', 'burdenManValue':'%4$s', 'clientNamesValue':'%5$s', 'linkManPhoneValue':'%6$s','renovateLicenceId':'%7$s','linkManValue':'%8$s','burdenManPhoneValue':'%9$s'}";
			String jsone = String.format(jsoneTmp, renovateRequisitionVO
					.getHouseId(), renovateRequisitionVO.getCompanyName(),
					renovateRequisitionVO.getRenovateLicenceNO(),
					renovateRequisitionVO.getBurdenMan(), renovateRequisitionVO
							.getClientNames(), renovateRequisitionVO
							.getLinkManPhone(), renovateRequisitionVO
							.getRenovateLicenceId(), renovateRequisitionVO
							.getLinkman(), renovateRequisitionVO
							.getBurdenManPhone());

			this.renderText(jsone);
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw re;
		}
	}

	/**
	 * 获取复验状态
	 *
	 * @param reinspectionId
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-8
	 */
	public void getReinspectionState(
			@Read(key = "reinspectionId") Long reinspectionId) {
		ReinspectionVO reinspectionVO = this.inspectionService
				.getReinspection(reinspectionId);
		if (reinspectionVO != null) {
			this.renderText(reinspectionVO.getState());
		}
	}

	/**
	 * 功能描述: 初验判断 <br>
	 *
	 * @param keyword Create author:许心炜 <br>
	 *            Create on:2009-1-13 <br>
	 *            Edit author: <br>
	 *            Edit on: <br>
	 *            Why: <br>
	 */
	public void hasFirstInspection(@Read(key = "keyword") Long keyword) {
		FirstInspectionVO firstInspectionVO = this.inspectionService
				.getFirstInspection(keyword);
		if (firstInspectionVO == null) {
			this.renderText("true");
		} else {
			this.renderText("false");
		}
	}

	/**
	 * 许可证判断
	 *
	 * @param renovateLicenceId
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-9
	 */
	public void hasInspectionRequisition(
			@Read(key = "renovateLicenceId") Long renovateLicenceId) {
		InspectionRequisitionVO inspectionRequisitionVO = this.inspectionService
				.getInspectionRequisitionByLicenceId(renovateLicenceId);
		if (inspectionRequisitionVO == null) {
			this.renderText("true");
		} else {
			this.renderText("false");
		}
	}

	/**
	 * 是否存在复验
	 *
	 * @param keyword
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-8
	 */
	public void hasReinspection(@Read(key = "keyword") Long keyword) {
		List<ReinspectionVO> list = this.inspectionService
				.getReinspections(keyword);
		if (list.size() > 0) {
			this.renderText("true");
		} else {
			this.renderText("false");
		}
	}

	/**
	 * 初始化列表
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-6
	 */
	public String initMainList() {

		InspectionRequisitionQueryConditionVO inspectionRequisitionQueryConditionVO = new InspectionRequisitionQueryConditionVO();

		this.setAttribute("inspectionRequisitionQueryConditionVO",
				inspectionRequisitionQueryConditionVO, Scope.SESSION);

		this.setAttribute("totalRows", new Integer(0));

		return this.LIST_PAGE;
	}

	/**
	 * 初始页面
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-24
	 */
	public String initPage() {
		// 房屋列表
		List<BuildingVO> buildingList = this.buildingService
				.getAllBuildingList();
		this.setAttribute("buildingList", buildingList);

		return this.INIT_PAGE;
	}

	/**
	 * 查询验收结果
	 *
	 * @param inspectionRequisitionQueryConditionVO
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-26
	 */
	public String queryInspectionRequisitionByPage(
			@Read(key = "ec_p", defaultValue = "1") Integer pageNO,
			@Read(key = "ec_crd", defaultValue = "15") Integer pageSize) {
		InspectionRequisitionQueryConditionVO inspectionRequisitionQueryConditionVO = (InspectionRequisitionQueryConditionVO) this
				.getAtrributeFromSeesion("inspectionRequisitionQueryConditionVO");
		if (inspectionRequisitionQueryConditionVO != null) {
			inspectionRequisitionQueryConditionVO.setPageNo(pageNO);
			inspectionRequisitionQueryConditionVO.setPageSize(pageSize);
		}

		inspectionRequisitionQueryConditionVO.setSortStr(ExtremeUtils
				.getOrderString(this.getRequest()));
		Page page = this.inspectionService
				.queryInspectionRequisitions(inspectionRequisitionQueryConditionVO);

		// TODO 模拟数据，过后删除
		// List<InspectionRequisitionQueryConditionVO> resultDataList = new
		// ArrayList<InspectionRequisitionQueryConditionVO>();
		// InspectionRequisitionQueryConditionVO tempVo = new
		// InspectionRequisitionQueryConditionVO();
		// tempVo.setKeyword(Long.valueOf(1));
		// tempVo.setHouseNumber("房号1253");
		// tempVo.setOwner("许心炜");
		// tempVo.setRenovateLicenceNO("15800000000");
		//
		// resultDataList.add(tempVo);

		// this.setAttribute("totalRows", new Integer(1));

		if (page != null) {
			List<InspectionRequisitionQueryConditionVO> tmpList = page
					.getResult();

			List<InspectionRequisitionQueryConditionVO> resultDataList = new ArrayList<InspectionRequisitionQueryConditionVO>();
			// TODO 预留处理数据逻辑
			for (InspectionRequisitionQueryConditionVO tmpVO : tmpList) {
				// 复验状态
				if ("001".equals(tmpVO.getReinspectionResult())) {
					tmpVO.setReinspectionResult("新建");
				}
				if ("002".equals(tmpVO.getReinspectionResult())) {
					tmpVO.setReinspectionResult("竣工初验");
				}
				if ("003".equals(tmpVO.getReinspectionResult())) {
					tmpVO.setReinspectionResult("复验不通过");
				}
				if ("004".equals(tmpVO.getReinspectionResult())) {
					tmpVO.setReinspectionResult("复验通过");
				}

				// 初验状态
				if ("001".equals(tmpVO.getFirstInspectionResult())) {
					tmpVO.setFirstInspectionResult("通过");
				}
				if ("002".equals(tmpVO.getFirstInspectionResult())) {
					tmpVO.setFirstInspectionResult("不通过");
				}

				resultDataList.add(tmpVO);
			}

			this.setAttribute("resultDataList", resultDataList);
			this.setAttribute("totalRows", Long.valueOf(page.getTotalCount())
					.intValue());
		} else {
			this.setAttribute("totalRows", new Integer(0));
		}

		return this.LIST_PAGE;
	}

	/**
	 * 查询页面
	 *
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-29
	 */
	public String searchPage() {
		// 房屋列表
		List<BuildingVO> buildingList = this.buildingService
				.getAllBuildingList();
		this.setAttribute("buildingList", buildingList);
		return this.SEARCH_PAGE;
	}

	/**
	 * @param buildingService the buildingService to set
	 */
	public void setBuildingService(IBuildingService buildingService) {
		this.buildingService = buildingService;
	}

	/**
	 * @param inspectionService the inspectionService to set
	 */
	public void setInspectionService(IInspectionService inspectionService) {
		this.inspectionService = inspectionService;
	}

	/**
	 * @param renovateManageService the renovateManageService to set
	 */
	public void setRenovateManageService(
			IRenovateManageService renovateManageService) {
		this.renovateManageService = renovateManageService;
	}

	/**
	 * 保存查询条件
	 *
	 * @param conditionVO
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-30
	 */
	public void setRenoViolateConditionCache(
			@Read InspectionRequisitionQueryConditionVO conditionVO) {
		this.setAttribute("condition", conditionVO, Scope.SESSION);
	}

	/**
	 * 保留查询条件
	 *
	 * @param inspectionRequisitionQueryConditionVO
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-4
	 */
	public void setSearchPramCatch(
			@Read InspectionRequisitionQueryConditionVO inspectionRequisitionQueryConditionVO) {
		this.setAttribute("inspectionRequisitionQueryConditionVO",
				inspectionRequisitionQueryConditionVO, Scope.SESSION);
		this.renderText("true");
	}

	/**
	 * 显示详细界面
	 *
	 * @param inspectionRequistionId
	 * @return
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-6
	 */
	public String showDetailPage(
			@Read(key = "inspectionRequistionId") @Out(key = "inspectionRequistionId") Long inspectionRequistionId,
			@Read(key = "renovateLicenceID") @Out(key = "renovateLicenceID") Long renovateLicenceID) {
		return this.DETAIL_PAGE;
	}

	/**
	 * 验证日期
	 *
	 * @param keyword
	 * @param dddDate
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2009-1-22
	 */
	public void validate(@Read(key = "keyword") Long keyword,
			@Read(key = "inspectionDateStr") String inspectionDateStr,
			@Read(key = "isFirst") boolean isFirst) {
		Date inspectionDate = DateUtil.convertStrToDate(inspectionDateStr,
				"yyyy-MM-dd hh时");
		if (isFirst) {
			FirstInspectionVO firstInspectionVO = this.inspectionService
					.getFirstInspection(keyword);
			if (firstInspectionVO.getInspectionDate().compareTo(
					inspectionDate) >= 0) {
				this.renderText("复验时间必须大于等于初验时间");
			} else {
				this.renderText("true");
			}
		} else {
			InspectionRequisitionVO inspectionRequisitionVO = this.inspectionService
					.getInspectionRequisition(keyword);
			if (inspectionRequisitionVO.getInspecteDate().compareTo(
					inspectionDate) >= 0) {
				this.renderText("初验时间必须大于等于验收申请时间");
			} else {
				this.renderText("true");
			}
		}
	}
}
